package com.hibikiluler.javalearning.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.hibikiluler.javalearning.entity.SysMenus;
import com.hibikiluler.javalearning.mapper.SysMenusMapper;
import com.hibikiluler.javalearning.service.SysMenusService;
import com.hibikiluler.javalearning.util.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Slf4j

public class SysMenusServiceImpl implements SysMenusService {

    @Autowired
    private SysMenusMapper sysMenusMapper;

    @Autowired
    private RedisUtils redisUtils;

    @Override
    public Result findParent() {
        return Result.success("查询父级菜单成功",sysMenusMapper.findParent());
    }

    @Override
    public Result insert(SysMenus sysMenus) {
        sysMenusMapper.insert(sysMenus);
        redisUtils.delKey("userInfo_" + SecurityUtils.getUsername());
        return Result.success("添加权限数据成功");
    }

    @Override
    public Result delete(Long id) {
        sysMenusMapper.delete(id);
        redisUtils.delKey("userInfo_" + SecurityUtils.getUsername());
        return Result.success("删除权限数据成功");
    }

    @Override
    public Result update(SysMenus sysMenus) {
        sysMenusMapper.update(sysMenus);
        redisUtils.delKey("userInfo_" + SecurityUtils.getUsername());
        return Result.success("修改权限数据成功");
    }

    @Override
    public Result findPage(Queryinfo queryinfo) {
        log.info("开始权限数据分页-->页吗{},-->页数{}-->查询内容{},",queryinfo.getPagenumber(),queryinfo.getPagesize(),queryinfo.getQueryString());
        //第一个参数为页码 第二个为页数大小
        PageHelper.startPage(queryinfo.getPagenumber(), queryinfo.getPagesize());
        Page<SysMenus> page = sysMenusMapper.findPage(queryinfo.getQueryString());
        long total = page.getTotal();
        List<SysMenus> result = page.getResult();
        log.info("查询的总条数-->{}",total);
        log.info("分页列表-->{}",result);
        return PageResult.pageResult(total,result);
    }
}
